<?php
/**
 * @package : TVBLog.
 * @class-name : TVB_Logger_Appender_Gearmand()
 * @author : stephen.mo <stephen.mo@achievo.com>
 * @date : 2010-03-25
 */

require_once 'TVB/Logger/Appender/Interface.php';
require_once 'TVB/Logger/Appender/requiresOfClient.php';

class TVB_Logger_Appender_GearmandClient implements TVB_Logger_Appender_Interface {
    public function write($log_entity) {
	$log_entity = (array)$log_entity;
        $gearmand_info = TVB_Logger_Config::get('gearmandjob', $log_entity['category']);
        $log_entitys = json_decode($log_entity['result'],true);
        $action = (!isset($log_entitys['action']) || $log_entitys['action']==1 || empty($log_entitys['action'])) ? 'do' : 'addTask';
        $level = (!isset($log_entitys['level']) || strtoupper($log_entitys['level'])=='H' || empty($log_entitys['level']) ) ? 'High' : ((strtoupper($log_entitys['level'])=='L') ? 'Low' : 'Normal');
        $style = (!isset($log_entitys['style']) || strtoupper($log_entitys['style'])=='B' || empty($log_entitys['style'])) ? 'Background' : '';

        //intergration the function name when you need to run or debug it.
        $function = $action.$level.$style;

        $servers = $gearmand_info['servers'];
	
		//write the queue to Job-Servers.
        $gmclient = createObjectOfClientJob::getInstance($servers);
        $result = $gmclient->{$function}('mstatWirteLogFromJobServers',$log_entitys['data']);
        if ($action!='do') {
            $result = $gmclient->runTasks();
        }

        //return the result;
        return $result;
    }

}

?>
